package com.hzlemone.form.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzlemone.form.model.request.FormItemRequestDTO;
import com.hzlemone.form.service.IFormItemService;
import com.hzlemone.mapper.model.FormItemMapper;
import com.hzlemone.mapper.model.db.FormItemDO;
import com.hzlemone.utils.CollectionMapperDecorator;

/**
 * @ClassName FormItemServiceImpl.java
 * @Description 表单项服务
 * @author Francis.LW
 * @date 2017年11月13日
 **/

@Service
@Transactional
public class FormItemServiceImpl implements IFormItemService {

	@Autowired
	CollectionMapperDecorator collectionMapperDecorator;

	@Autowired
	FormItemMapper formItemMapper;

	@Override
	public PageInfo<FormItemDO> listFormItemByPage(FormItemDO param) {
		if (param.getPage() != null && param.getRows() != null) {
			PageHelper.startPage(param.getPage(), param.getRows(),
					param.getSort());
		}
		List<FormItemDO> formInfoDOs = formItemMapper.listFormItem(param);
		PageInfo<FormItemDO> p = new PageInfo<FormItemDO>(formInfoDOs);
		return p;
	}

	@Override
	public List<FormItemDO> listFormItem() {
		return formItemMapper.listFormItem(new FormItemDO());
	}

	@Override
	public List<FormItemDO> listFormItemByFormId(String formId) {
		FormItemDO formItemDO = new FormItemDO();
		formItemDO.setFormId(formId);
		return formItemMapper.listFormItem(formItemDO);
	}

	@Override
	public void saveFormItem(FormItemRequestDTO formItemRequestDTO) {
		FormItemDO formItemDO = collectionMapperDecorator.map(
				formItemRequestDTO, FormItemDO.class);

		formItemDO.setCreatedTime(new Date());
		formItemMapper.insert(formItemDO);
	}

	@Override
	public FormItemDO getFormItemById(String id) {
		return formItemMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateFormItem(String id, FormItemRequestDTO formItemRequestDTO) {
		FormItemDO formItemDO = collectionMapperDecorator.map(
				formItemRequestDTO, FormItemDO.class);

		formItemDO.setId(id);
		formItemMapper.insert(formItemDO);
	}

	@Override
	public void removeFormItem(String[] ids) {
		for (String id : ids) {
			formItemMapper.deleteByPrimaryKey(id);
		}
	}

}
